//
//  DHLeftBarVC.m
//  DHKuaiYi
//
//  Created by Cedric Zheng on 13/11/2016.
//  Copyright © 2016 30-陈辉华. All rights reserved.
//

#import "DHLeftBarVC.h"
#import "DHLeftBarTopView.h"
#import "DHListModel.h"
#import "DHBaseNavController.h"
#import <Masonry.h>
#import <UIViewController+MMDrawerController.h>

#define SELF_WIDTH self.mm_visibleDrawerFrame.size.width

@interface DHLeftBarVC () <UITableViewDelegate>

@property (weak, nonatomic) DHLeftBarTopView *topView;

@end

@implementation DHLeftBarVC

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];

    
#warning Should remove later
    [XHHUserDefaultsTool saveObject:nil forKey:CURRENT_LOGIN_ACCOUNT_KEY];
    
    //监听登陆状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveLoginStatusChanged:) name:LOGIN_STATUS_CHANGED object:nil];
    
    self.tableView.delegate = self;
    self.tableView.bounces = NO;
}

- (void)xhhSetupSubviews {
    [super xhhSetupSubviews];
    
    /*----设置顶部视图----*/
    DHLeftBarTopView *topV = [[DHLeftBarTopView alloc] initWithFrame:CGRectMake(0.0, 0.0, SELF_WIDTH, SELF_WIDTH * 0.33)];
    
    self.topView = topV;
    [topV.icon addTarget:self action:@selector(topViewIconDidTapped) forControlEvents:UIControlEventTouchUpInside];
    [topV.userNameOrLogin addTarget:self action:@selector(userNameOrLoginDidTapped) forControlEvents:UIControlEventTouchUpInside];
    [topV.basicInfoOrRegister addTarget:self action:@selector(basicInfoOrRegisterDidTapped) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:topV];
    
    /*----设置列表----*/
    [self configureTableViewBaseOnLoginStatus];
    
    //对列表布局做出调整
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topV.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
}

#pragma mark - Events
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)didReceiveLoginStatusChanged:(id)sender {
    [self configureTopViewBaseOnLoginStatus];
    [self configureTableViewBaseOnLoginStatus];
}

- (void)topViewIconDidTapped {
    [self centerVCPushControllerWithVCName:@"DHUserInfoViewController"];
}

- (void)userNameOrLoginDidTapped {
    DHBaseNavController *NavCon = [[DHBaseNavController alloc] initWithRootViewController:[self getControllerWithClassName:@"DHLoginViewController"]];
    
    [self presentViewController:NavCon animated:YES completion:^{
    }];
}

- (void)basicInfoOrRegisterDidTapped {
    DHBaseNavController *NavCon = [[DHBaseNavController alloc] initWithRootViewController:[self getControllerWithClassName:@"DHRegisterViewController"]];
    
    [self presentViewController:NavCon animated:YES completion:^{
    }];
}

#pragma mark – Private Methods

/**
 根据登陆状态来设置顶部视图
 */
- (void)configureTopViewBaseOnLoginStatus {
    if ([XHHUserDefaultsTool objetForKey:CURRENT_LOGIN_ACCOUNT_KEY]) {
        self.topView.hasLogin = YES;
    } else {
        self.topView.hasLogin = NO;
    }
    [self.topView layoutSubviews];
}

/**
 根据登陆状态来设置下方列表
 */
- (void)configureTableViewBaseOnLoginStatus {
    if ([XHHUserDefaultsTool objetForKey:CURRENT_LOGIN_ACCOUNT_KEY]) {
        [self loadDataListWithName:@"LeftBarList.plist"];
    } else {
        [self loadDataListWithName:nil];
    }
}

- (void)centerVCPushControllerWithVCName:(NSString *)pushVCName {
    [self.mm_drawerController closeDrawerAnimated:YES completion:nil];
    
    UIViewController *vc = [self getControllerWithClassName:pushVCName];
    vc.hidesBottomBarWhenPushed = YES;
    
    UITabBarController *tabBarController = (UITabBarController *)self.mm_drawerController.centerViewController;
    [tabBarController.selectedViewController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate
/**
 选中行之后，在centerController推出对应的VC。

 @param tableView tableView
 @param indexPath indexPath
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 取消选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSArray *modelArr = self.dataList[indexPath.section];
    DHListModel *model = modelArr[indexPath.row];
    NSString *pushVCName = model.pushVCName;
    
    if (pushVCName) {
        [self centerVCPushControllerWithVCName:pushVCName];
        //如果没有pushVCName就创建一个空白VC
    } else {
        [self.mm_drawerController closeDrawerAnimated:YES completion:nil];
        
        UIViewController *emptyVC = [UIViewController new];
        emptyVC.view.backgroundColor = [UIColor whiteColor];
        emptyVC.hidesBottomBarWhenPushed = YES;
        
        UITabBarController *tabBarController = (UITabBarController *)self.mm_drawerController.centerViewController;
        [tabBarController.selectedViewController pushViewController:emptyVC animated:YES];
    }
}

#pragma mark - <#Datasources#>

#pragma mark – Getters and Setters

@end
